package com.cgc.shop.listener;

import java.util.Timer;

import javax.annotation.Resource;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.cgc.shop.task.CommendProductTask;

public class InitHomeDataListener implements ServletContextListener {
	
	private ApplicationContext context;

	private CommendProductTask commendProductTask;
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
		context = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
		commendProductTask = (CommendProductTask) context.getBean("commendProductTask");
		commendProductTask.setApplicationContext(event.getServletContext());
		
		//定时获取热门类别中的推荐商品放在application属性中供首页页面使用
        //通过设置定时器，让首页的数据每个一小时同步一次（配置为守护线程）  
        new Timer(true).schedule(commendProductTask, 0, 1000*60*60);//每个一小时执行一次commendProductTask任务
		
	}

}
